<?php

namespace Sharks\Core\LifeCycle;

use Sharks\Core\AppContext;
use Sharks\Core\Http\Cookie;
use Sharks\Core\Http\Flash;
use Sharks\Core\Http\Form\FormFiles;
use Sharks\Core\Http\Form\FormToObj;
use Sharks\Core\Http\Form\FormValidator;
use Sharks\Core\Http\Request;
use Sharks\Core\Http\Response;
use Sharks\Core\Http\Session;
use Sharks\Core\Persistence\Model;
use stdClass;

class DefaultParamResolver extends ActionParamResolver {

	public function resolve($name, $type) {
		$app = AppContext::instance();
		if (AppContext::class == $type) {
            return $app;
        }
        elseif (Request::class == $type) {
            return $app->request();
        }
        elseif (Response::class == $type) {
            return $app->response();
        }
        elseif (Cookie::class == $type) {
            return $app->cookie();
        }
        elseif (Session::class == $type) {
            return $app->session();
        }
        elseif (Flash::class == $type) {
            return $app->flash();
        }
        elseif (FormValidator::class == $type) {
            return new FormValidator;
        }
        elseif (FormFIles::class == $type) {
            return new FormFiles();
        }
        elseif (is_subclass_of($type, Model::class)) {
            $obj = new $type;
            FormToObj::generate($obj, $name);
            return $obj;
        }
        elseif ($type == null) {
            $obj = new stdClass;
            FormToObj::generate($obj, $name);
            if (empty(get_object_vars($obj))) {
                return null;
            }
            return $obj;
        }
        else {
            return null;
        }
	}
}